// 挂号
function registered(name, callback ) {
  console.log(name + '挂号中');
  setTimeout( function(){
      // todo
    callback(4);
  }, 2000  );
}


// 看医生
function seeDoctor(name, callback ) {
  console.log(name + '正在看医生');
  setTimeout( function(){
    callback('一包板蓝根');
  }, 3000  );
}

// 买药
function takeMedicine(name, callback ) {
  console.log(name + '正在拿药');
  setTimeout( function(){
    callback();
  }, 1000 );
}

// registered('小明', function(num){
//   console.log('挂号成功，号码牌：',num );
//   seeDoctor('小明', function(m) {
//     console.log('医生要小明拿药：',m );
//     takeMedicine('小明', function(){
//       console.log('拿完药，看病结束');
//     })
//   })
// })

// 将普通函数转换成 Promise 化的函数
const promisify = (fun) => {
    const promisifyFun = (arg) => {
        return new Promise( (resolve) => {
            fun(arg, resolve)
        } );
    }
    return promisifyFun;
}

const registeredPromiseify = promisify(registered);
const seeDoctorPromiseify =  promisify(seeDoctor);
const takeMedicinePromiseify = promisify(takeMedicine);

const step = async () => {

    const user = '小明';
    const num = await registeredPromiseify(user);
    console.log('挂号成功，号码牌：',num );

    const m = await seeDoctorPromiseify(user);
    console.log('医生要小明拿药：',m );

    await takeMedicinePromiseify(user);

    return 33;

}

step().then((data) => {

    console.log('END', data);

});